
module JConsole
    def JConsole.start(port=3000) 
        cmd =<<-EOCMD.split("\n").join(" ")
            jconsole
            -J-Dcom.sun.management.jmxremote 
            -J-Dcom.sun.management.jmxremote.port=#{port} 
            -J-Dcom.sun.management.jmxremote.ssl=false 
            -J-Dcom.sun.management.jmxremote.authenticate=false
        EOCMD

        Thread.start { system cmd } 
        sleep 2
    end

    def JConsole.stop()
        jconsole_pid = `ps a -o pid,command | grep -w jconsole |  grep -v grep | grep -v ruby | cut -c -5`
        `kill #{jconsole_pid}` if jconsole_pid != ""
        sleep 2
    end
end

if ARGV.length == 1
    case ARGV[0]
    when "start"
        JConsole::start
        puts "started jconsole"
    when "stop"
        JConsole::stop
        puts "stopped jconsole"
    end
end
